package com.duanjw.springframework;

import org.apache.catalina.Context;
import org.apache.catalina.LifecycleException;
import org.apache.catalina.WebResourceRoot;
import org.apache.catalina.startup.Tomcat;
import org.apache.catalina.webresources.DirResourceSet;
import org.apache.catalina.webresources.StandardRoot;

public class MySpringBootApplication {
    public static void run(){
        try {
            System.out.println("启动tomcat");
            Tomcat tomcat = new Tomcat();
            tomcat.setPort(8081);
            String path = MySpringBootApplication.class.getResource("/").getPath();
            Context context = tomcat.addWebapp("", "/Users/duanjw/webapps");

            WebResourceRoot standardRoot = new StandardRoot(context);
            standardRoot.addPreResources(new DirResourceSet(standardRoot,"/WEB-INFO/classes",path,"/"));

            context.setResources(standardRoot);
            // Tomcat 9.0 必须调用 Tomcat#getConnector() 方法之后才会监听端口
            tomcat.getConnector();
            tomcat.start();
            tomcat.getServer().await();
        } catch (LifecycleException e) {
            e.printStackTrace();
        }
    }
}
